package com.xhu.util;


import java.nio.charset.StandardCharsets;
import java.util.BitSet;
import java.util.Random;
import java.util.UUID;

public class Main {
    //隐藏的东东
    static byte[] DBuffer = "wo shi yi ge fen shua jiang".getBytes();
    //存储原始html信息
    static byte[] CBuffer ;
    //存储加入了隐藏信息的html
    static byte[] yBuffer ;

    public static void main(String[] args) {
        initDBuffer();
        initCBuffer();
        String encoded=informationHiding();
        System.out.println("加密结果:"+encoded);
        String decoded=informationRead(encoded);
        System.out.println("解密结果:"+decoded);
        System.out.println(UUID.randomUUID().toString());
    }

    /**
     * 提取隐藏信息
     * @param encoded
     * @return
     */
    private static String informationRead(String encoded) {
        byte[] source=encoded.getBytes(StandardCharsets.UTF_8);
        StringBuilder sb=new StringBuilder();
        //解析成二进制数据存放到字符串
        for (int i = 0; i < source.length; i++) {
            if(source[i]=='='){
                sb.append(source[i-1]==' '?1:0);
                sb.append(source[i+1]==' '?1:0);
            }
        }

        //二进制bit解析成字节信息
        byte[] result=new byte[sb.length()/8];
        for (int i = 0; i < sb.length()/8; i++) {
            String bin=sb.substring(i*8,i*8+8);
            System.out.println(bin);
            result[i]=Byte.parseByte(bin,2);
        }

        //生成解析结果字符串信息
        String resultStr=new String(result,StandardCharsets.UTF_8);
        System.out.println(resultStr);
        return resultStr;
    }

    /**
     * 生成加入了隐藏信息的html字符串
     * @return  返回加入了隐藏信息的字符串
     */
    private static String informationHiding() {
        int j = 0;
        int k = 0;
        for (int i = 0; i < CBuffer.length; i++) {
            if (CBuffer[i] == '=') {
                if (DBuffer[k] == '1') {
                    yBuffer[j] = ' ';
                    j++;
                    yBuffer[j] = CBuffer[i];
                    j++;
                    k++;

                    if(DBuffer[k] == '1')
                    {
                        yBuffer[j] = ' ';
                        j++;
                    }
                    k++;
                } else {
                    yBuffer[j] = CBuffer[i];
                    j++;
                    k++;

                    if(DBuffer[k] == '1')
                    {
                        yBuffer[j] = ' ';
                        j++;
                    }
                    k++;
                }
            }else
            {
                yBuffer[j] = CBuffer[i];
                j++;
            }
        }
        System.out.println("hideStr:\n"+new String(yBuffer));
        return new String(yBuffer);
    }


    /**
     * 初始化加密信息， 加密信息字符串转为二进制信息存储
     */
    private static void initDBuffer() {
        byte[] DBuffer2=new byte[DBuffer.length*8];
        for (int i = 0; i < DBuffer.length; i++) {
            StringBuilder sb=new StringBuilder(Integer.toBinaryString(DBuffer[i]));
            int len=sb.length();
            for (int j = 0; j < 8-len; j++) {
                sb.insert(0,'0');
            }
            System.arraycopy(sb.toString().getBytes(),0,DBuffer2,i*8,8);
        }
        DBuffer=new String(DBuffer2).getBytes();
        System.out.println(new String(DBuffer2));
    }

    /**
     * 初始化html数据信息，加入足够多的=标记
     */
    private static void initCBuffer() {
        StringBuilder sb = new StringBuilder();
        sb.append("<html><body>");
        for (int i = 0; i < DBuffer.length/2; i++) {
            sb.append("<p id='" + i + "'>" + i + "</p>");
        }
        sb.append("</body></html>");
        CBuffer = sb.toString().getBytes(StandardCharsets.UTF_8);
        yBuffer=new byte[CBuffer.length + DBuffer.length/2];
    }
}
